package bme.andraskindler.treenventory.provider;

import java.io.IOException;

import bme.andraskindler.treenventory.TreenventoryApplication;
import bme.andraskindler.treenventory.activity.OptionsActivity;
import bme.andraskindler.treenventory.imagemanipulation.Filters;
import bme.andraskindler.treenventory.model.MeasureResult;
import bme.andraskindler.treenventory.widget.CameraSurfaceView;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.view.SurfaceView;

/**
 * Háttérben futó kamerát testesít meg, mely az előnézeti kép megjelenítése nélkül tud fényképezni. A másodlagos telefon használja.
 * @author andraskindler
 *
 */
public class CameraProvider {

	private Camera mCamera;
	private static CameraProvider mInstance;

	private CameraProvider() {
		mCamera = Camera.open();
		if (mCamera != null) {
			Parameters params = mCamera.getParameters();
			params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
			params.setJpegQuality(1);
			mCamera.setParameters(params);
			SurfaceView view = new SurfaceView(TreenventoryApplication.getInstance());
			try {
				mCamera.setPreviewDisplay(view.getHolder());
				mCamera.startPreview();
			} catch (IOException e) {
				e.printStackTrace();
			}
			mInstance = this;

		} else
			System.out.println("no camera");
	}

	public static CameraProvider getInstance() {
		if (mInstance == null)
			mInstance = new CameraProvider();
		return mInstance;
	}

	/**
	 * Ez a függvény felel a fénykép elkészítéséért, majd meghívja a {@link Filters} osztály mérőfüggvényét.
	 * A paraméterek beállítása után fókuszál, majd elkészíti a képet.
	 */
	public void takePhoto() {

		final SharedPreferences preferences = TreenventoryApplication.getInstance().getSharedPreferences(OptionsActivity.SETTINGS, Context.MODE_PRIVATE);

		final Parameters params = mCamera.getParameters();
		params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

		final int size = preferences.getInt(OptionsActivity.SETTINGS_RESOLUTION, 1);
		switch (size) {
		case CameraSurfaceView.RESOLUTION_SMALL:
			params.setPictureSize(320, 240);
			break;
		case CameraSurfaceView.RESOLUTION_NORMAL:
			params.setPictureSize(640, 480);
			break;
		case CameraSurfaceView.RESOLUTION_LARGE:
			params.setPictureSize(1280, 960);
			break;
		default:
			break;
		}
		mCamera.setParameters(params);

		mCamera.autoFocus(new AutoFocusCallback() {

			public void onAutoFocus(boolean success, Camera camera) {
				mCamera.takePicture(null, null, null, new Camera.PictureCallback() {

					public void onPictureTaken(byte[] data, Camera camera) {

						final Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length);
						SharedPreferences preferences = TreenventoryApplication.getInstance().getSharedPreferences(OptionsActivity.SETTINGS, Context.MODE_PRIVATE);
						MeasureResult result = Filters.measureTrunk(photo, preferences.getInt(OptionsActivity.SETTINGS_HEIGHT, 25), OptionsActivity.SETTINGS_TRESHOLD, preferences.getInt(OptionsActivity.SETTINGS_ACCURACY, 1), Filters.CAMERA_OTHER);

						TreenventoryApplication.getInstance().getmService().write((Double.toString(result.getLeftEndX()) + " " + Double.toString(result.getLeftEndY()) + " " + Double.toString(result.getLeftEndZ()) + " " + Double.toString(result.getLeftStartX()) + " " + Double.toString(result.getLeftStartY()) + " " + Double.toString(result.getRightEndX()) + " " + Double.toString(result.getRightEndY()) + " " + Double.toString(result.getRightEndZ())+ " " + Double.toString(result.getRightStartX())+ " " + Double.toString(result.getRightStartY())).getBytes());
					}

				});
			}

		});

	}
}
